﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CefSharp;
using KeZhou.ViewModels;
using KeZhou.Models.Instances;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using CefSharp.Wpf;
using System.Reactive.Joins;
using KeZhou.Models.Wishire;
using KeZhou.Models;

namespace KeZhou.Views.Components.Controls
{
    /// <summary>
    /// MainThreeInfo.xaml 的交互逻辑
    /// </summary>
    public partial class MainThreeInfo : Page
    {
        private static MainThreeInfo instance;

        public static MainThreeInfo Instance
        {
            get
            {
                return instance ?? (instance = new MainThreeInfo());
            }
        }

        public MainThreeInfo()
        {

            InitializeComponent();
            instance = this;
            this.DataContext = InfoViewModel.Instance;

            // 允许以同步的方式注册C#的对象到JS中
            Browser.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
            // 禁用函数名变换
            Browser.JavascriptObjectRepository.NameConverter = null;

            // 注册C#对象到JS中
            Browser.JavascriptObjectRepository.Register("ViewModel", InfoViewModel.Instance, options: BindingOptions.DefaultBinder);
            InfoViewModel.Instance.setBrowser(Browser);

            // 在地图上初始化兴趣点
            // init_Browser();

            /*以下为资讯详情的交互*/
            InfoBrowser.Address = "http://101.35.231.245:1003/#/ThreePage";
            /*InfoBrowser.Address = "http://localhost:1003/#/ThreePage";*/
            InfoBrowser.JavascriptObjectRepository.Settings.LegacyBindingEnabled = true;
            InfoBrowser.JavascriptObjectRepository.NameConverter = null;

            InfoBrowser.JavascriptObjectRepository.Register("bound", InfoViewModel.Instance, options: BindingOptions.DefaultBinder);

        }

        private void init_Browser(object sender, EventArgs e)
        {
            JObject info = new JObject();
            info["city"] = PlanOnBuilding.Instance.cityName;
            JArray addressList = new JArray();
            List<POI> poiList = InfoViewModel.Instance.FoodData.Concat(InfoViewModel.Instance.PlaceData).Concat(InfoViewModel.Instance.MustData).ToList();
            foreach (POI poi in poiList)
            {
                JObject address = new JObject();
                address["name"] = poi.Name;
                address["type"] = poi.Type;
                address["lng"] = poi.Lng;
                address["lat"] = poi.Lat;
                addressList.Add(address);
            }
            info["address"] = addressList;
            var jsCode = $"init({info})";
            Browser.ExecuteScriptAsyncWhenPageLoaded(jsCode);
        }

        private void Browser_Loaded(object sender, RoutedEventArgs e)
        {
            Browser.Load(Environment.CurrentDirectory + "/Map/Info/map.html");
        }
    }
}
